package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.entity.Log;
import com.atguigu.jxc.service.GoodsService;

import com.atguigu.jxc.service.LogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author :ttm
 * @description
 */
@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private GoodsDao goodsDao;
    @Autowired
    private LogService logService;

    @Override
    public Map<String, Object> getlistInventory(Integer page, Integer rows, String codeOrName, Integer goodsTypeId) {
        List<Goods> goods = goodsDao.getlistInventory(codeOrName, goodsTypeId);
        HashMap<String, Object> map = new HashMap<>();
        map.put("rows", goods);
        return map;
    }

    @Override
    public Map<String, Object> list(Integer page, Integer rows, String goodsName, Integer goodsTypeId) {
        page = page == 0 ? 1 : page;
        int offSet = (page - 1) * rows;

        HashMap<String, Object> map = new HashMap<>();
        List<Goods> goodsList = goodsDao.list(goodsName, goodsTypeId);
        map.put("rows", goodsList);

        return map;
    }

    @Override
    public ServiceVO save(Goods goods) {
        if (goods.getGoodsId() == null) {
            logService.save(new Log(Log.INSERT_ACTION, "添加商品" + goods.getGoodsName()));
            goods.setLastPurchasingPrice(goods.getPurchasingPrice());
            goods.setInventoryQuantity(0);
            goods.setState(0);
            goodsDao.saveGoods(goods);
        } else {
            goodsDao.updateGoods(goods);
            logService.save(new Log(Log.INSERT_ACTION, "修改商品" + goods.getGoodsName()));
        }

        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);

    }

    @Override
    public ServiceVO delete(Integer goodsId) {
        Goods goods = goodsDao.findByGoodsId(goodsId);
        if (goods.getState() == 1) {
            return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
        } else if (goods.getState() == 2) {
            return new ServiceVO<>(ErrorCode.HAS_FORM_ERROR_CODE, ErrorCode.HAS_FORM_ERROR_MESS);
        } else {
            logService.save(new Log(Log.DELETE_ACTION, "删除商品:" + goods.getGoodsName()));
            goodsDao.deleteGoods(goodsId);
        }
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    @Override
    public Map<String, Object> getNoInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        HashMap<String, Object> map = new HashMap<>();
        page = page == 0 ? 1 : page;
        int offSet = (page - 1) * rows;
        List<Goods> goodsList = goodsDao.getNoInventoryQuantity(offSet, rows, nameOrCode);
        map.put("rows", goodsList);
        map.put("total", goodsDao.getNoInventoryQuantityCount(nameOrCode));
        logService.save(new Log(Log.SELECT_ACTION, "分页查询商品信息（无库存）"));
        return map;
    }

    @Override
    public Map<String, Object> getHasInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        HashMap<String, Object> map = new HashMap<>();
        page = page == 0 ? 1 : page;
        int offSet = (page - 1) * rows;
        List<Goods> goodsList = goodsDao.getHasInventoryQuantity(offSet, rows, nameOrCode);
        map.put("rows", goodsList);
        map.put("total", goodsDao.getHasInventoryQuantityCount(nameOrCode));
        logService.save(new Log(Log.SELECT_ACTION, "分页查询商品信息(有库存)"));
        return map;
    }

    @Override
    //添加库存、修改数量或成本价
    public ServiceVO saveStock(Integer goodsId, Integer inventoryQuantity, double purchasingPrice) {
        Goods goods = goodsDao.findByGoodsId(goodsId);

        goods.setInventoryQuantity(inventoryQuantity);

        goods.setPurchasingPrice(purchasingPrice);

        goods.setLastPurchasingPrice(purchasingPrice);

        goodsDao.updateGoods(goods);

        logService.save(new Log(Log.UPDATE_ACTION, goods.getGoodsName() + "商品期初入库"));

        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    @Override
    //要判断商品状态 入库、有进货和销售单据的不能删除
    public ServiceVO deleteStock(Integer goodsId) {
        Goods goods = goodsDao.findByGoodsId(goodsId);
        if (goods.getState()==2){
            return new ServiceVO<>(ErrorCode.HAS_FORM_ERROR_CODE, ErrorCode.HAS_FORM_ERROR_MESS);
        }
        goods.setInventoryQuantity(0);
        goodsDao.updateGoods(goods);
        logService.save(new Log(Log.UPDATE_ACTION,goods.getGoodsName()+"商品清除库存"));

        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);

        }

    @Override
    public Map<String, Object> listAlarm() {
        HashMap<String, Object> map = new HashMap<>();
        List<Goods> goodsList=goodsDao.getGoodsAlarm();
        map.put("rows",goodsList);
        logService.save(new Log(Log.SELECT_ACTION, "查询库存报警商品信息"));
        return map;
    }
}


